<?php

class PdocListAction extends CommonAction {
    
    public function index(){
        
        
        $db_pdoc = M('Pdoc');
        $db_m_membersPdoc = M('MMembersPdoc');
        $uid = $_SESSION['current_member']['uid'];
        $pdocs = $db_pdoc->where('pdoc_isdel=0')->order('pdoc_createtime desc')->select();
        
        for($i=0; $i<count($pdocs);$i++){
            //$pdocs[$i]['last_date'] = $db_m_membersPdoc->where(array('uid'=>$uid, 'pdoc_id'=>$pdocs[$i]['pdoc_id']))->getField('last_date');
            $pdocs[$i]['time_study'] = $db_m_membersPdoc->where(array('uid'=>$uid, 'pdoc_id'=>$pdocs[$i]['pdoc_id']))->sum('time_study');
        }
        

        $this->assign("pdocs", $pdocs);
        
        
        $this->display();
    }
    
    

    public function index_bak() {
        $db_pdoc = M('Pdoc');
//        $pdocs = $db_pdoc->order('pdoc_createtime desc,id asc')->select();
//
//        $this->assign('pdocs', $pdocs);

        $db_dictionary = M('Dictionary');
        $db_m_membersPdoc = M('MMembersPdoc');
        $pid = $db_dictionary->where(array('name' => 'pdoc_catagory'))->getField('id');
        $catagories = $db_dictionary->where(array('pid' => $pid))->order(array('order' => 'asc'))->select();
        $uid = $_SESSION['current_member']['uid'];
        
        for ($i = 0, $j = 0; $i < count($catagories); $i++) {
            $catagory = $catagories[$i]['name'];
            $res = $db_pdoc->where(array('pdoc_catagory' => $catagory, 'pdoc_isdel'=>0))->order('pdoc_createtime desc')->select();
            for($k=0; $k<count($res); $k++){
                $res[$k]['time_study'] = $db_m_membersPdoc->where(array('uid'=>$uid, 'pdoc_id'=>$res[$k]['pdoc_id']))->getField('time_study');
            }
            if(!empty($res)){
                $pdocsArr[$j++] = $res;
            }
        }

        //dump($pdocsArr);

        $this->assign('pdocsArr', $pdocsArr);
        $this->display();
    }

}
